/**
 * This file is part of aion-core <aioncore.com>
 *
 *  aion-core is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  aion-core is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with aion-core.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.aionemu.loginserver.network.gameserver.clientpackets;

import com.aionemu.loginserver.GameServerInfo;
import com.aionemu.loginserver.controller.AccountController;
import com.aionemu.loginserver.network.gameserver.GsClientPacket;
import com.aionemu.loginserver.network.gameserver.GsConnection;

import java.nio.ByteBuffer;

/**
 * @author xavier
 *		 <p/>
 *		 Packet sent to game server to request account characters count
 *		 When all characters count have been received, send server list to client
 */
public class CM_GS_CHARACTER_COUNT extends GsClientPacket {
	private int accountId;
	private int characterCount;

	/**
	 * @param buf
	 * @param client
	 */
	public CM_GS_CHARACTER_COUNT(ByteBuffer buf, GsConnection client) {
		super(buf, client, 0x07);
	}

	@Override
	protected void readImpl() {
		accountId = readD();
		characterCount = readC();
	}

	@Override
	protected void runImpl() {
		GameServerInfo gsi = getConnection().getGameServerInfo();

		AccountController.addCharacterCountFor(accountId, gsi.getId(), characterCount);

		if (AccountController.hasAllCharacterCounts(accountId)) {
			AccountController.sendServerListFor(accountId);
		}
	}
}
